;
(function() {
  new Vue({
    el: '#ddTEAMconfig',
    data: {
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      isflag: false, //钉钉解绑弹框
      isShowQyLayer: false, //企业绑定弹框
      corpConfigFlag: 'N', //是否绑定企业
      agentId: '',
      appKey: '',
      appSecret: '',
      bindStatus: 'T', //绑定返回状态（T文本，S成功，E失败）
      resultsText: '',
      serviceInfo: '',
      optionList: [],
      corp_type: '',
      dictName: '请选择',
      selectedArea: false,
    },
    methods: {
      //配置跳转
      configTarge: function(val, id) {
        hlUrlUtil.goTo('ddConfig', {
          'id': this.serviceInfo.id,
          'ddtab': true
        })
      },
      validateAgentId: function() {
        var self = this;
        if (!self.agentId) {
          hl.toast('请填写微应用ID')
          return false;
        }
      },
      validateAppKey: function() {
        var self = this;
        if (!self.appKey) {
          hl.toast('请填写授权Key')
          return false;
        }
      },
      validateAppSecret: function() {
        var self = this;
        if (!self.appSecret) {
          hl.toast('请填写授权Secret')
          return false;
        }
      },
      validateCorpSecret: function() {
        var self = this;
        // 企业绑定新增了微应用 AppSecret ， 暂时不需要验证企业数据真实性
        // if (self.validateQyForm()) sendValidateRequest()
        function sendValidateRequest() {
          hlhttp.put({
            url: 'user/corp_info/_check',
            data: {
              'corp_id': self.corpId,
              'corp_secret': self.corpSecret
            },
            success: function(data) {
              hl.toast(data.appDesc)
            }
          })
        }
      },
      validateQyForm: function() {
        var self = this;
        if (!self.agentId) {
          hl.toast('请填写微应用ID')
          $('.agentId-ipt').focus()
          return false;
        }
        if (!self.appKey) {
          hl.toast('请填写授权Key')
          $('.corpSecret-ipt').focus()
          return false;
        }
        if (!self.appSecret) {
          hl.toast('请填写授权Secret')
          $('.corpSecret-ipt').focus()
          return false;
        }
        return true;
      },
      // 绑定第三方企业信息
      bindQyInfo: function() {
        var self = this;
        if (!self.validateQyForm()) return false;
        hlhttp.post({
          url: 'user/corp_info/_bind',
          data: {
            'agent_id': self.agentId,
            'app_key': self.appKey,
            'corp_secret': self.appSecret,
            'corp_type': self.corp_type
          },
          success: function(data) {
            self.isShowQyLayer = false;
            self.bindStatus = 'S';
            self.resultsText = '绑定成功'
            self.corpConfigFlag = 'Y'
            var userMes = JSON.parse(window.localStorage.getItem('userMes'));
            userMes['corpConfigFlag'] = 'Y'
            window.localStorage.setItem('userMes', JSON.stringify(userMes))
            self.getservice_info()
          },
          error: function(data) {
            data.responseText = JSON.parse(data.responseText);
            self.bindStatus = 'E';
            self.resultsText = data.responseText.msg
          }
        })
      },
      confirmBindQy: function() {
        if (this.dictName == '请选择') {
          hl.toast('请选择微应用类型')
          return
        }
        this.bindQyInfo()
      },
      closeQyLayer: function() {
        this.isShowQyLayer = false;
      },
      closeAccountLayer: function() {
        this.isShowAccountLayer = false;
      },
      //关闭弹框
      closeFlag() {
        this.isflag = false
      },
      // 获取第三方服务信息
      getservice_info() {
        var that = this
        hlhttp.get({
          url: 'tenant/corp/services',
          success: function(data) {
            that.serviceInfo = data[0]
          },
        })
      },
      // 获取corp_type字典
      getoption() {
        var that = this
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'CNFG',
            config_name: 'CF_SERVICE_CORP_TYPE'
          },
          success: function(data) {
            that.optionList = data
          },
        })
      },
      // 选中字典
      selectedict(val, name) {
        this.corp_type = val
        this.dictName = name
        this.selectedArea = false
      },
      //解绑
      server_untying() {
        var that = this
        hlhttp.put({
          url: 'user/dd_server_untying',
          success: function(data) {
            that.isflag = false
            that.corpConfigFlag = 'N'
            var userMes = JSON.parse(window.localStorage.getItem('userMes'));
            userMes['corpConfigFlag'] = 'N'
            window.localStorage.setItem('userMes', JSON.stringify(userMes))
          },
        })
      },
    },
    mounted: function() {
      this.getservice_info();
      this.getoption();
    },
    beforeMount: function() {
      this.corpConfigFlag = JSON.parse(window.localStorage.getItem('userMes')).corpConfigFlag
    }
  })
}())
